package a_接口调用凭证.a_获取接口调用凭据;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import my.wechat.rc.common.HttpClientUtil;
import my.wechat.rc.common.JsonUtil;
import my.wechat.rc.common.MapUtil;
import org.joda.time.DateTime;

import java.io.BufferedWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;

/**
 * @author 86153
 * @date 2022/7/28
 **/
public class GetAccessToken {

    public static final String appid = "wx007c003f01379703";
    public static final String secret = "4f263832bd0160777ddb555098492c85";

    public static GetAccessTokenResponse getAccessToken() {
        try {
            Path path = Paths.get("_db/access_token.db");
            if (Files.exists(path)) {
                String jsonStr = new String(Files.readAllBytes(path), Charset.defaultCharset());
                GetAccessTokenResponse getAccessTokenResponse = JsonUtil.strToObj(jsonStr, GetAccessTokenResponse.class);
                if (getAccessTokenResponse.getExpiresTime().after(new Date())) {
                    return getAccessTokenResponse;
                }
            } else {
                Files.createFile(path);
            }
            GetAccessTokenResponse getAccessTokenResponse = HttpClientUtil.get("https://api.weixin.qq.com/cgi-bin/token",
                    MapUtil.of("grant_type", "client_credential",
                            "appid", appid,
                            "secret", secret), GetAccessTokenResponse.class);
            getAccessTokenResponse.setExpiresTime(new DateTime().plusSeconds(getAccessTokenResponse.expiresIn - 1).toDate());
            BufferedWriter bufferedWriter = Files.newBufferedWriter(path, Charset.defaultCharset());
            bufferedWriter.write(JsonUtil.objToStr(getAccessTokenResponse));
            bufferedWriter.flush();
            bufferedWriter.close();
            return getAccessTokenResponse;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new GetAccessTokenResponse();
    }

    public static void main(String[] args) {
        getAccessToken();
    }

    public static class GetAccessTokenResponse {
        @JsonProperty("access_token")
        private String accessToken;
        @JsonProperty("expires_in")
        private Integer expiresIn;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss:SSS")
        private Date expiresTime;

        public String getAccessToken() {
            return accessToken;
        }

        public void setAccessToken(String accessToken) {
            this.accessToken = accessToken;
        }

        public Integer getExpiresIn() {
            return expiresIn;
        }

        public void setExpiresIn(Integer expiresIn) {
            this.expiresIn = expiresIn;
        }

        public Date getExpiresTime() {
            return expiresTime;
        }

        public void setExpiresTime(Date expiresTime) {
            this.expiresTime = expiresTime;
        }
    }
}
